<template>
    <input-select :id="id" :styles="styles"  :sign="sign" :time="time"  :valsql="valsql" :isreadonly="isreadonly" :index="index" isChange="Y" :isRequired="isRequired"  :data="data"  @input="changeData" :exkey="exkey" :selectData="selectData" @selectdata="selectdata"></input-select>
</template>
<script>
import InputSelect  from '../input/input-select'
export default {
    props:['id','styles','value','data','index','appid','valsql','sign','time','selectData','exkey','isRequired','isreadonly'],
    name: "LcSelectNew",
    components: {
        InputSelect
    },
     data(){
        return{
            options:[],
            lt_dat1:"",
            lt_dat2:"",
            
            sfItemData:[],
            lg_ex1old:"",
            pId:"",
            cId:"",
            lg_ex3old:"",
            history:{},
            historyItem:{},
            historyHas:{},
            historyHour:{},
            days:0,
            hours:0,
            startDates:"",
            endDates:"",
            endLgEx3:""
        }
    },
    created(){
         this.$post("bygex","byg/funeral/getFuneralSfItem",{}).then(data=>{
            console.log(data,"ata===")
             this.sfItemData=[];
            for(let i=0;i<data.item.length;i++){
                this.sfItemData.push(data.item[i]);
            }
            console.log(this.sfItemData,"ss")
            console.log("created===========================")
            this.initHistory();
            this.changeAmt();
            this.computerHistory();
         });
        let ids=this.id.split(".");
        this.pId=ids[0];
    },
    watch:{
         data:{
            handler: function (newVal) {
                this.changeAmt();
               
            },
            deep: true    //深度监听
         }
    },
    methods:{
        initHistory(){
          //  try{
                console.log(this.data,"initHistroy================")
                this.history={}
                this.historyHour={}
                let preEndDate="";
                let nextStartDate="";
                if(this.data["lcHistory"]&&this.data["lcHistory"].length>0){
                    let endDates=this.data["lcHistory"][this.data["lcHistory"].length-1].lc_dat4;
                    if(endDates){
                        endDates=endDates.substring(0,10);
                    }
                    this.endDates=endDates;
                    this.endLgEx3=this.data["lcHistory"][this.data["lcHistory"].length-1].sf_ex51
                    this.data["lcHistory"].forEach((item,index) => {
                        let lc_dat1=item.lc_dat1;
                        let lc_dat4=item.lc_dat4;
                        let lg_ex3=item.lg_ex3;
                        let sf_ex51=item.sf_ex51;
                        let sf_ex8=item.sf_ex8;
                        nextStartDate=this.data["lcHistory"].length-1>index ? this.data["lcHistory"][index+1].lc_dat1 :""
                        if(nextStartDate){
                            nextStartDate=nextStartDate.substring(0,10);
                        }
                        if(index==0){
                            this.startDates=lc_dat1;
                        }
                        let lc_dat1_=lc_dat1.substring(0,10);
                        let lc_dat4_=lc_dat4.substring(0,10);
                        var stime =new Date(lc_dat1).getTime();
                        var etime = new Date(lc_dat4).getTime();
                        var usedTime = etime - stime;  //两个时间戳相差的毫秒数
                        let hours=Math.floor(usedTime/(3600*1000));

                        let stime_ =new Date(lc_dat1_).getTime();
                        var etime_ = new Date(lc_dat4_).getTime();
                        var usedTime_ = etime_ - stime_;  //两个时间戳相差的毫秒数
                        let days=Math.floor(usedTime_/(24*3600*1000));
                        let hours2=hours-days*24;
                        let days_=0;

                        if(hours2<0){
                            days_=-1;
                            hours2=hours<24 ? hours:(hours-(days-1)*24)
                        }
                        
                       
                        let nums=0;
                        if(this.history[lg_ex3]){
                            nums=this.history[lg_ex3]
                        }
                       
                        if(sf_ex8.indexOf("天")!=-1||sf_ex8.indexOf("日")!=-1){
                            if(sf_ex51=="自然天数"&&lc_dat1&&lc_dat4){
                                if(preEndDate!=lc_dat1_)
                                    days++;
                            }
                                
                            let lc_dat1_hour=lc_dat1.substring(11,13);
                            let lc_dat4_hour=lc_dat4.substring(11,13);
                            if(sf_ex51=="12点后算半天"&&lc_dat1&&lc_dat4){
                                if(nextStartDate!=lc_dat4_){
                                     //  alert(lt_dat1_hour)
                                    if(lc_dat1_hour>12){
                                        days=days+0.5;
                                    }else{
                                        days++;
                                    }
                                }
                               
                            }
                            if(sf_ex51=="不到12点算半天"&&lc_dat1&&lc_dat4){
                                if(nextStartDate!=lc_dat4_){
                                     //  alert(lt_dat1_hour)
                                    if(lc_dat4_hour<12){
                                        days=days+0.5;
                                    }else{
                                        days++;
                                    }
                                }
                               
                            }
                            if(sf_ex51=="不足12小时算半天"){
                                console.log("days_===="+days_+"=="+hours2)
                                days=days+days_;
                                let h=0;
                                if(this.historyHour[lg_ex3])
                                    h=this.historyHour[lg_ex3]
                                this.historyHour[lg_ex3]=h+hours2;
                               
                            }
                            if(days_>=0)
                                days=days<=0 ? 1:days;
                            nums+=days;
                            if(sf_ex51!="不足12小时算半天"){
                                
                                this.days+=days;
                            }
                           

                            console.log("initDate==="+lc_dat1+"=="+lc_dat4+"=="+days+"=="+sf_ex51+"=="+lc_dat4_hour+"=="+hours2);
                        }else{
                            hours=hours<=0 ? 1:hours;
                            nums+=hours;
                            this.hours+=hours;
                        }
                        preEndDate=lc_dat4_;
                        this.history[lg_ex3]=nums;
                        
                    });
                    for(let key in this.historyHour){
                        let days=this.history[key];
                        let hour=this.historyHour[key];
                        let d=Math.floor(hour/24);
                        console.log("hours==============="+hour+"="+d)
                        hour=hour-d*24;
                        if(hour>0&&hour<12){
                            days=days+0.5
                        }else if(hour>=12){
                            days=days+1;
                        }
                        days=days+d;
                        this.days+=days;
                        this.history[key]=days;
                        this.historyHour[key]=hour;
                    }
                    let mgs="";
                    if(this.days){
                        mgs=this.days+"天"
                    }
                    if(this.hours){
                        mgs+=this.hours+"小时"
                    }
                    //alert(mgs+"=="+this.startDates)
                    this.$set(this.data["lc"],"startDates",this.startDates);
                    this.$set(this.data["lc"],"nums",mgs);

                    this.historyItem={};
                    for(let i=0;i<this.sfItemData.length;i++){
                        if(this.history[this.sfItemData[i].szi_ex1]){
                        
                            this.historyItem[this.sfItemData[i].szi_ex1]=JSON.parse(JSON.stringify(this.sfItemData[i]))
                        }
                    }
                }

          //  }catch(e){}
        },
        changeAmt(){
            if(this.sfItemData.length==0){
                return;
            }
            let lt_dat1=this.data[this.pId]["lc_dat1"];
            let lt_dat2=this.data[this.pId]["lc_dat4"];
            let lc_ex1=this.data[this.pId]["lc_ex1"];
            let lg_ex3="";
            for(let i=0;i<this.options.length;i++){
                let optvalue=this.options[i].optvalue;
                let lg_ex3_=this.options[i].lg_ex3;
                if(optvalue==lc_ex1){
                    lg_ex3=lg_ex3_;
                }
            } 
          //  console.log(this.options,"options====")
          //  console.log("lc_ex1===================="+lc_ex1+"|lc_dat1=="+lt_dat1+"|lt_dat2=="+lt_dat2+"|lt_dat22=="+this.lt_dat2+"=="+this.pId)
            if(lc_ex1&&(lt_dat1!=this.lt_dat1||lt_dat2!=this.lt_dat2||this.lg_ex1old!=lc_ex1)){
          //      console.log("计算======================")
                this.lg_ex1old=lc_ex1
                this.lt_dat1=lt_dat1;
                this.lt_dat2=lt_dat2;
                let days=0;
                let hours=0;
                let hours2=0;
                let days_=0;
                console.log("lt_dat1==="+lt_dat1+"=="+lt_dat2)
                if(lt_dat1&&lt_dat2){
                    
                    let lt_dat1_=lt_dat1.substring(0,10);
                    let lt_dat2_=lt_dat2.substring(0,10);
                    var stime =new Date(lt_dat1).getTime();
                    var etime = new Date(lt_dat2).getTime();
                    var usedTime = etime - stime;  //两个时间戳相差的毫秒数
                    hours=Math.floor(usedTime/(3600*1000));

                    let stime_ =new Date(lt_dat1_).getTime();
                    var etime_ = new Date(lt_dat2_).getTime();
                    var usedTime_ = etime_ - stime_;  //两个时间戳相差的毫秒数
                    days=Math.floor(usedTime_/(24*3600*1000));
                    hours2=hours-days*24;
                    console.log("days==="+days+"=="+hours2+"=="+hours)
                    if(hours2<0){
                        days_=-1;
                        hours2=hours<24 ? hours:(hours-(days-1)*24)
                    }
                    
                    //this.$emit("input",data);
                    
                }
                let isOK=false;
                let deleteIndex=-1;
                for(let i=0;i<this.data["item"].length;i++){
                    if(this.lg_ex3old&&this.lg_ex3old!=lg_ex3&&this.data["item"][i].szi_ex1==this.lg_ex3old){
                        deleteIndex=i;
                    }
                    console.log("item==="+this.data["item"][i].szi_ex1+"=="+lg_ex3)
                    if(this.data["item"][i].szi_ex1==lg_ex3){
                        let row_=this.data["item"][i];
                       // console.log("szi_ex28====="+row_.szi_ex28)
                        if(row_.szi_ex3.indexOf("天")!=-1||row_.szi_ex3.indexOf("日")!=-1){

                            let nums=0;
                            if(this.history[lg_ex3]){
                                nums=this.history[lg_ex3]
                            }
                            if(row_.szi_ex28=="自然天数"&&lt_dat1&&lt_dat2){
                                if(nums<=0||this.endDates!=lt_dat1_){
                                    days++;
                                }
                                
                            }
                               
                                let lt_dat1_hour=lt_dat1.substring(11,13);
                                let lt_dat2_hour=lt_dat2.substring(11,13);
                            if(row_.szi_ex28=="12点后算半天"&&lt_dat1&&lt_dat2){
                                //  alert(lt_dat1_hour)
                                if(nums<=0||this.endDates!=lt_dat1_){
                                    if(lt_dat1_hour>12){
                                        days=days+0.5;
                                    }else{
                                        days++;
                                    }
                                }
                                
                            }
                            if(row_.szi_ex28=="不到12点算半天"&&lt_dat1&&lt_dat2){//这里没有考虑换柜等情况，可能会多一天
                                //  alert(lt_dat1_hour)
                                if(lt_dat2_hour<12){
                                    days=days+0.5;
                                }else{
                                    days++;
                                }
                            }
                            if(row_.szi_ex28=="不足12小时算半天"&&lt_dat1&&lt_dat2&&hours2>0){
                                //  alert(lt_dat1_hour)
                                console.log("days=sss=========="+days+"=="+days_)
                                days=days+days_;
                                if(nums<=0){
                                    if(hours2<12){
                                        days=days+0.5;
                                    }else{
                                        days++;
                                    }
                                }else{
                                    let n=this.historyHour[lg_ex3];
                                    console.log("nnn==="+days+"=="+n+"=="+hours2+"=="+nums)
                                    if(n>0&&n<12){
                                        nums=nums-0.5;
                                    }else if(n>=12){
                                        nums=nums-1;
                                    }
                                    n=n+hours2;
                                    if(n>24){
                                        n=n-24;

                                        nums=nums+1;
                                    }
                                    if(n>0&&n<12){
                                        nums=nums+0.5;
                                    }else if(n>=12){
                                        nums=nums+1;
                                    }
                                }
                               
                            }
                            if(nums<=0)
                                days=days<=1 ? 1:days;
                          
                           
                            days+=nums;
                           /* if(this.endDates==lt_dat1){
                                days=days-1;
                            }*/
                            days=days<=0 ? 1:days;
                            days=days<nums ? nums:days;

                            console.log("days======="+days+"=="+nums)

                            let szi_ex2=Number(row_.szi_ex2);
                            row_.szi_ex4=days;
                            row_.szi_ex5=days*szi_ex2
                            this.$set(this.data["item"],i,row_);

                            this.$set(this.data[this.pId],"lc_ex15",days)
                            isOK=true;
                            break;
                        }else{
                            hours=hours<=0 ? 1:hours;
                            let nums=0;
                            if(this.history[lg_ex3]){
                                nums=this.history[lg_ex3]
                            }
                            hours+=nums;
                            /*if(this.endDates==lt_dat1){
                                hours=hours-1;
                            }*/
                            hours=hours<=0 ? 1:hours;
                            hours=hours<nums ? nums:hours;
                            let szi_ex2=Number(row_.szi_ex2);
                            row_.szi_ex4=hours;
                            row_.szi_ex5=hours*szi_ex2
                            this.$set(this.data[this.pId],"lc_ex15",hours)
                            this.$set(this.data["item"],i,row_);
                            isOK=true;
                            break;
                        }
                        
                    }
                }
                if(deleteIndex!=-1){
                    if(this.history[this.lg_ex1old]){
                        let nums=this.history[this.lg_ex1old];
                        let szi_ex2=Number(this.data["item"][deleteIndex].szi_ex2);
                        let szi_ex5=nums*szi_ex2;
                        this.$set(this.data["item"][deleteIndex],"szi_ex4",nums);
                        this.$set(this.data["item"][deleteIndex],"szi_ex5",szi_ex5);
                    }else{
                        this.$delete(this.data["item"],deleteIndex);
                        console.log(this.data["item"],"itemssssss")
                    }
                    
                }
                this.lg_ex3old=lg_ex3;
                console.log("isOk=============================="+isOK+"=="+this.sfItemData.length+"=="+lg_ex3)
                
                if(!isOK){
                    console.log(this.sfItemData,"this.sfItemData=22==========================")
                    console.log("lg_ex3=================="+lg_ex3)
                   
                    this.historyHas={}
                    for(let i=0;i<this.sfItemData.length;i++){
                       
                       
                        if(this.sfItemData[i].szi_ex1==lg_ex3){
                            let row_=this.sfItemData[i];
                            this.historyHas[lg_ex3]="Y"
                            
                            if(row_.szi_ex3.indexOf("天")!=-1||row_.szi_ex3.indexOf("日")!=-1){
                                let nums=0;
                                if(this.history[lg_ex3]){
                                    nums=this.history[lg_ex3]
                                }
                                if(row_.szi_ex28=="自然天数"&&lt_dat1&&lt_dat2)
                                    days++;
                                let lt_dat1_hour=lt_dat1.substring(11,13);
                                let lt_dat2_hour=lt_dat2.substring(11,13);
                                if(row_.szi_ex28=="12点后算半天"&&lt_dat1&&lt_dat2){
                                //  alert(lt_dat1_hour)
                                    if(lt_dat1_hour>12){
                                        days=days+0.5;
                                    }else{
                                        days++;
                                    }
                                }
                                if(row_.szi_ex28=="不到12点算半天"&&lt_dat1&&lt_dat2){
                                    //  alert(lt_dat1_hour)
                                    if(lt_dat2_hour<12){
                                        days=days+0.5;
                                    }else{
                                        days++;
                                    }
                                }
                                if(row_.szi_ex28=="不足12小时算半天"&&lt_dat1&&lt_dat2&&hours2>0){
                                    //  alert(lt_dat1_hour)
                                    days=days+days_;
                                    if(nums<=0){
                                        if(hours2<12){
                                            days=days+0.5;
                                        }else{
                                            days++;
                                        }
                                    }else{
                                        let n=this.historyHour[lg_ex3];
                                        if(n>0&&n<12){
                                            nums=nums-0.5;
                                        }else if(n>=12){
                                            nums=nums-1;
                                        }
                                        n=n+hours2;
                                        if(n>24){
                                            n=n-24;

                                            nums=nums+1;
                                        }
                                        if(n>0&&n<12){
                                            nums=nums+0.5;
                                        }else if(n>=12){
                                            nums=nums+1;
                                        }
                                    }
                                
                                }
                                days=days<=1 ? 1:days;

                               
                                days+=nums;
                                /*if(this.endDates==lt_dat1){
                                    days=days-1;
                                }*/
                                days=days<=0 ? 1:days;
                                days=days<nums ? nums:days;
                                let szi_ex2=Number(row_.szi_ex2);
                                row_.szi_ex4=days;
                                row_.szi_ex5=days*szi_ex2
                                this.$set(this.data[this.pId],"lc_ex15",days)
                            }else{
                                hours=hours<=0 ? 1:hours;
                                let nums=0;
                                if(this.history[lg_ex3]){
                                    nums=this.history[lg_ex3]
                                }
                                hours+=nums;
                                /*if(this.endDates==lt_dat1){
                                    hours=hours-1;
                                }*/
                                hours=hours<=0 ? 1:hours;
                                hours=hours<nums ? nums:hours;
                                let szi_ex2=Number(row_.szi_ex2);
                                row_.szi_ex4=hours;
                                row_.szi_ex5=hours*szi_ex2
                                this.$set(this.data[this.pId],"lc_ex15",hours)
                            }
                            
                            this.$set(this.data["item"],this.data["item"].length,row_);
                            break;
                        }
                    }
                }
                
            }
            
        },
        computerHistory(){
            console.log(this.history,"this.history=========")
            this.data["item"].forEach(item => {
                let szi_ex1=item.szi_ex1;
                this.historyHas[szi_ex1]="Y"
            });
            for(let key in this.history){
                let num=this.history[key]
                if(num&&!this.historyHas[key]){
                    let row_=this.historyItem[key];
                    console.log(row_,"row_==================")
                    let szi_ex2=Number(row_.szi_ex2);
                    row_.szi_ex4=num;
                    row_.szi_ex5=num*szi_ex2
                    console.log("addItem======================================")
                    this.$set(this.data["item"],this.data["item"].length,row_);
                }
            }
        },
        changeData(data){
            let item=JSON.parse(data);
            this.data[this.pId]["lc_ex1"]=item.val;
            this.changeAmt();
            
        },
        selectdata(data){
            //alert(2)
            console.log(data,"selectdata========================")
            this.options=JSON.parse(data)
            console.log(this.options,"options")
             let lc_ex1=this.data["lc"]["lc_ex1"];
            
             for(let i=0;i<this.options.length;i++){
                let optvalue=this.options[i].optvalue;
                if(optvalue==lc_ex1){
                    this.lg_ex1old=optvalue;
                }
            }
            
            this.changeAmt();
        },
    }
}
</script>